package cn.wjx.taobaounion.presenter.impl;

import cn.wjx.taobaounion.model.Api;
import cn.wjx.taobaounion.model.domain.TicketParam;
import cn.wjx.taobaounion.model.domain.TicketResult;
import cn.wjx.taobaounion.presenter.ITicketPresenter;
import cn.wjx.taobaounion.utils.LogUtils;
import cn.wjx.taobaounion.utils.RetrofitManager;
import cn.wjx.taobaounion.utils.URLUtils;
import cn.wjx.taobaounion.view.ITicketPageCallback;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static java.net.HttpURLConnection.HTTP_OK;

/**
 * @author WuChangJian
 * @date 2020/6/22 21:22
 */
public class TicketPresenter implements ITicketPresenter {

    private ITicketPageCallback mCallback;
    private TicketResult mTicketResult;
    private String mCover;

    enum LoadState {
        SUCCESS, ERROR, LOADING, NONE
    }
    private LoadState mCurrentState = LoadState.NONE;

    @Override
    public void getTicket(String title, String cover, String url) {
        this.mCover = cover;
        mCurrentState = LoadState.LOADING;
        String ticketPath = URLUtils.getTicketPath(url);
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        TicketParam ticketParam = new TicketParam(ticketPath, title);
        Call<TicketResult> call = api.getTicketResult(ticketParam);
        call.enqueue(new Callback<TicketResult>() {
            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                if (response.code() == HTTP_OK) {
                    mTicketResult = response.body();
                    // 通知UI更新
                    mCurrentState = LoadState.SUCCESS;
                    onTicketLoadedSuccess();
                } else {
                    mCurrentState = LoadState.ERROR;
                    onTicketLoadedError();
                    LogUtils.d(this, "response code --> " + response.code());
                }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                onTicketLoadedError();
                LogUtils.d(this, "onFailure --> " + t.getMessage());
            }
        });
    }

    private void onTicketLoadedError() {
        mCurrentState = LoadState.ERROR;
        if (mCallback != null) {
            mCallback.onError();
        }
    }

    private void onTicketLoadedSuccess() {
        mCurrentState = LoadState.SUCCESS;
        if (mCallback != null) {
            mCallback.onTicketLoaded(mCover, mTicketResult);
        }
    }

    @Override
    public void registerViewCallback(ITicketPageCallback callback) {
        // 说明这时候，数据提前到达，界面还没显示
        this.mCallback = callback;
        if (mCurrentState != LoadState.NONE) {
            if (mCurrentState == LoadState.SUCCESS) {
                callback.onTicketLoaded(mCover, mTicketResult);
            } else if (mCurrentState == LoadState.ERROR) {
                callback.onError();
            } else if (mCurrentState == LoadState.LOADING) {
                callback.onLoading();
            }
        }
    }

    @Override
    public void unregisterViewCallback(ITicketPageCallback callback) {
        if (mCallback != null) {
            this.mCallback = null;
        }
    }
}
